package server.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import message.GroupJoinRequestMessage;
import message.GroupJoinResponseMessage;
import server.session.GroupSession;
import server.session.GroupSessionFactory;

@ChannelHandler.Sharable
public class GroupJoinRequestMessageHandler extends SimpleChannelInboundHandler<GroupJoinRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, GroupJoinRequestMessage groupJoinRequestMessage) throws Exception {
        String groupName = groupJoinRequestMessage.getGroupName();
        GroupSession groupSession = GroupSessionFactory.getGroupSession();
        groupSession.joinMember(groupJoinRequestMessage.getUsername(), groupName);
        channelHandlerContext.writeAndFlush(new GroupJoinResponseMessage(true, "加入成功"));
    }
}
